﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bzxxxt.bll;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// bg_hzbyyear 的摘要说明
    /// </summary>
    public class bg_hzMonthByZhengYear : rm_base
    {
        HzBLL hzBLL = new HzBLL();
        public override void getall(HttpContext context)
        {
            string op = context.Request.Params["op"];
            if (string.IsNullOrEmpty(op))
            {
                //全市或镇骨灰堂年度统计汇总        
                string stryear = context.Request.Params["year"];
                string type = context.Request.Params["type"];
                string zheng = context.Request.Params["areacode"];
                int? year = null;
                if (stryear == null)
                {
                    year = DateTime.Now.Year;
                }
                else if (stryear != "")
                {
                    year = int.Parse(stryear);
                }
                string json = hzBLL.hzMonthParadiseByUserTypeZhengYear(year, type, zheng);
                context.Response.Write(json);
            }
            else {
                //单个骨灰堂分年度按区汇总
                string pcode = context.Request.Params["paradisecode"];
                string pname = "";
                string stryear = context.Request.Params["year"];
                var p = hzBLL.getParadise(pcode);
                if (p != null)
                {
                    pname = "【" + p.name.Trim() + "】";
                }

                int? year = null;
                if (stryear == null)
                {
                    year = DateTime.Now.Year;
                }
                else if (stryear != "")
                {
                    year = int.Parse(stryear);
                }
                string json = hzBLL.hzMonthSectionByUserParadiseYear(pcode, year);
                context.Response.Write(json);
            }
        }

        public override void other(HttpContext context)
        {
            string action = context.Request.Params["action"];
            switch (action)
            {                               
                case "export":
                    {
                        string op = context.Request.Params["op"];
                        if (string.IsNullOrEmpty(op))
                        {
                            //全市或镇骨灰堂年度统计结果导出        
                            string type = context.Request.Params["type"];
                            string zheng = context.Request.Params["areacode"];
                            string stryear = context.Request.Params["year"];
                            string city = hzBLL.getAreaNameByCode(zheng);
                            int? year = null;
                            if (stryear == null)
                            {
                                year = DateTime.Now.Year;
                            }
                            else if (stryear != "")
                            {
                                year = int.Parse(stryear);
                            }
                            HttpResponse resp = context.Response;
                            resp.Charset = "utf-8";
                            resp.Clear();
                            string filename = stryear + "年度" + city + "入葬情况统计表_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                            resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
                            resp.ContentEncoding = System.Text.Encoding.UTF8;

                            resp.ContentType = "application/ms-excel";
                            string style = "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>"
                                + "<style> .table{ font: 13pt Tahoma, Verdana; color: #000000; text-align:center;border-collapse:collapse;border:solid #000 1px; } "
                                + ".table td{text-align:center; border:solid #000 1px;} "
                                + ".table th{text-align:center; font-weight: bold; padding:5px; border:solid #000 1px;} </style>";
                            resp.Write(style);

                            resp.Write("<table class='table' border='1'><tr><th colspan='14'>" + stryear + "年度" + city + "入葬情况统计表</th></tr><tr><th>骨灰堂名称</th><th>01月</th><th>02月</th><th>03月</th><th>04月</th><th>05月</th><th>06月</th><th>07月</th><th>08月</th><th>09月</th><th>10月</th><th>11月</th><th>12月</th><th>全年</th></tr>");
                            var list = hzBLL.GetHzMonthParadiseByUserTypeZhengYearData(int.Parse(stryear), type, zheng);
                            for (int i = 0; i < list.Count(); i++)
                            {
                                string row = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td><td>{12}</td><td>{13}</td></tr>",
                                     list[i].pname, list[i].m1, list[i].m2, list[i].m3, list[i].m4, list[i].m5, list[i].m6, list[i].m7, list[i].m8, list[i].m9, list[i].m10, list[i].m11, list[i].m12, list[i].mm);
                                resp.Write(row);
                            }
                            resp.Write("</table>");
                            resp.Flush();
                        }
                        else {
                            //单个骨灰堂内部年度统计结果导出                        
                            string pcode = context.Request.Params["paradisecode"];
                            string pname = "";
                            string stryear = context.Request.Params["year"];
                            var p = hzBLL.getParadise(pcode);
                            if (p != null)
                            {
                                pname = "【" + p.name + "】";
                            }

                            int? year = null;
                            if (stryear == null)
                            {
                                year = DateTime.Now.Year;
                            }
                            else if (stryear != "")
                            {
                                year = int.Parse(stryear);
                            }
                            HttpResponse resp = context.Response;
                            resp.Charset = "utf-8";
                            resp.Clear();
                            string filename = stryear + "年度" + pname + "入葬情况统计表_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                            resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
                            resp.ContentEncoding = System.Text.Encoding.UTF8;

                            resp.ContentType = "application/ms-excel";
                            string style = "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>"
                                + "<style> .table{ font: 13pt Tahoma, Verdana; color: #000000; text-align:center;border-collapse:collapse;border:solid #000 1px; } "
                                + ".table td{text-align:center; border:solid #000 1px;} "
                                + ".table th{text-align:center; font-weight: bold; padding:5px; border:solid #000 1px;} </style>";
                            resp.Write(style);

                            resp.Write("<table class='table' border='1'><tr><th colspan='14'>" + stryear + "年度" + pname + "入葬情况统计表</th></tr><tr><th>骨灰堂名称</th><th>01月</th><th>02月</th><th>03月</th><th>04月</th><th>05月</th><th>06月</th><th>07月</th><th>08月</th><th>09月</th><th>10月</th><th>11月</th><th>12月</th><th>全年</th></tr>");
                            var list = hzBLL.GetHzMonthSectionByUserParadiseYearData(pcode, int.Parse(stryear));
                            for (int i = 0; i < list.Count(); i++)
                            {
                                string row = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td><td>{12}</td><td>{13}</td></tr>",
                                     list[i].pname, list[i].m1, list[i].m2, list[i].m3, list[i].m4, list[i].m5, list[i].m6, list[i].m7, list[i].m8, list[i].m9, list[i].m10, list[i].m11, list[i].m12, list[i].mm);
                                resp.Write(row);
                            }
                            resp.Write("</table>");
                            resp.Flush();
                        }
                        break;
                    }                
            }
        }
    }
}